package com.um2.compression.grol;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;


public class PanneauImageFinaleCrominanceBleuRouge extends JPanel {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int prefferedSizeX = 200 ;
	private int prefferedSizeY = 200 ;
	private Image image = null ;
	private PanneauCentre panneauCentre = null ;
	
	private int coinSuperieurX = 0 ;
	private int coinSuperieurY = 0 ;
	private List<Rectangle> rectanglesInclus = new ArrayList<Rectangle> () ;
	private List<Integer> ValeursIncluses = new ArrayList<Integer> () ;
	
	public PanneauImageFinaleCrominanceBleuRouge(PanneauCentre centre) {
		super(true) ;
		this.panneauCentre = centre ;
	}
	
	
	
	
	
	
	public void setImage(Image img) {
		this.image = img ;
	}
	
	
	
	
	
	
	
	public void recalculeImage() {
		
		
		this.rectanglesInclus = new ArrayList<Rectangle> () ;
		this.ValeursIncluses = new ArrayList<Integer> () ;
		
		if (this.getImageOriginale()!=null)
		{
	
			BufferedImage buLuminance = Utilitaire.toBufferedImage(this.panneauCentre.getPanneauLuminance().getImage()) ;
		
			this.image = buLuminance ;
			
			
			BufferedImage bufferFINALLUMINANCE = new BufferedImage(buLuminance.getWidth(),
					buLuminance.getHeight(),
					 BufferedImage.TYPE_INT_ARGB);
			
			for (int i = 0; i < buLuminance.getWidth(); i++) {
				for (int j = 0; j < buLuminance.getHeight(); j++) {
					if (this.panneauCentre.getPanneauImageFinaleCrominanceBleu().getMatricePixelsExplores()[i][j])
					{
						bufferFINALLUMINANCE.setRGB(i, j, (new Color(247,60,60)).getRGB()) ;
					}else {
						bufferFINALLUMINANCE.setRGB(i, j, 0x00FF0000) ;
					}
				}
			}
			
			this.image = bufferFINALLUMINANCE ;
		}
	}

	
	
	
	
	
	
	
	@Override
	public Dimension getPreferredSize() {
		if (this.image==null)
			return new Dimension(this.prefferedSizeX,this.prefferedSizeY) ;
		return new Dimension(this.image.getWidth(this),
				this.image.getHeight(this)) ;
	}

	
	
	
	

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		
		this.dessineGrillageDeFond(g) ;
		
		
		if (this.getImage()==null)
			return ;
		
		coinSuperieurX = (this.getWidth()-this.getImage().getWidth(this))/2 ;
		if (coinSuperieurX<0)
			coinSuperieurX = -coinSuperieurX ;
		
		coinSuperieurY = (this.getHeight()-this.getImage().getHeight(this))/2 ;
		if (coinSuperieurY<0)
			coinSuperieurY = -coinSuperieurX ;
		
		g.drawImage(this.getImage(), coinSuperieurX, coinSuperieurY, this) ;
		
	}


	private void dessineGrillageDeFond(Graphics g) {
		int largeur = this.getWidth() ;
		if (this.getImage()!=null)
			largeur = this.getImage().getWidth(this) ;
		
		int hauteur = this.getHeight() ;
		if (this.getImage()!=null)
			hauteur = this.getImage().getHeight(this) ;
		
		
		if (this.getImage()!=null) {
			coinSuperieurX = (this.getWidth()-this.getImage().getWidth(this))/2 ;
			if (coinSuperieurX<0)
				coinSuperieurX = -coinSuperieurX ;
			
			coinSuperieurY = (this.getHeight()-this.getImage().getHeight(this))/2 ;
			if (coinSuperieurY<0)
				coinSuperieurY = -coinSuperieurX ;

			g.clipRect(coinSuperieurX, coinSuperieurY,
					this.getImage().getWidth(this),
					this.getImage().getHeight(this)) ;
		}
		
		boolean couleurClaire = true ;
		boolean couleurClaireInitiale = true ;
		// la largeur
		for (int i = 0; i < largeur; i=i+10) {
			// la hauteur
			couleurClaire = couleurClaireInitiale ;
			couleurClaireInitiale = !couleurClaireInitiale ;
			for (int j = 0; j < hauteur; j=j+10) {
				if (couleurClaire)
				{
					g.setColor(new Color(0x777777)) ;
				}else {
					g.setColor(new Color(0xBBBBBB)) ;
				}
				g.fillRect(coinSuperieurX+i, coinSuperieurY+j, 10, 10) ;
				couleurClaire = !couleurClaire ;
			}
		}
		
		g.clipRect(0, 0,
				this.getWidth(),
				this.getHeight()) ;
		
	}


	public Image getImage() {
		return image;
	}
	
	public Image getImageOriginale() {
		return this.panneauCentre.getPanneauImageOriginale().getImage() ;
	}






	public List<Rectangle> getRectangles() {
		return this.rectanglesInclus;
	}






	public List<Integer> getValeurs() {
		return this.ValeursIncluses;
	}
	

}
